//
//  ReweetedStatusView.swift
//  Weibo
//
//  Created by 张远文 on 16/4/30.
//  Copyright © 2016年 张远文. All rights reserved.
//

import UIKit
import SnapKit

class ReweetedStatusView: UIView {

    //原创微博底部约束  用来记录底部底部约束,后续根据是否有配图,动态修改
    var bottomConstraint: Constraint?
    
    //数据模型
    var retweetedStatus: Status? {
        
        didSet {
            retweetedLabel.attributedText = EmoticonManager.emoticonManager.textToImageText(retweetedStatus?.text ?? "", font: retweetedLabel.font)
            
            //移除约束条件
            self.bottomConstraint?.uninstall()
            
            //给配图设置数据源
            if let picurls = retweetedStatus?.imageURLs where picurls.count > 0 {
                //有配图视图
                pictureView.imageURLs = picurls
                
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
                })
            } else {
                //没有配图视图
                //更新约束
                //隐藏配图视图
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomConstraint = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(statusCellMargin).constraint
                })
            }
            
        }
    }
    
    //MARK: - 重写构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 设置UI
    private func setupUI() {
        
        //添加子视图
        addSubview(retweetedLabel)
        addSubview(pictureView)
        
        //设置约束
        retweetedLabel.snp_makeConstraints { (make) in
            make.top.equalTo(snp_top).offset(statusCellMargin)
            make.left.equalTo(snp_left).offset(statusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(retweetedLabel.snp_left)
        }
        
        self.snp_makeConstraints { (make) in
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
        }
    }
    
    //MARK: - 懒加载控件
    private lazy var retweetedLabel: UILabel = UILabel(title: "我是转发微博", size: 14, color: UIColor.darkGrayColor(), margin: statusCellMargin)
    private lazy var pictureView: StatusPictureView = StatusPictureView()
}
